/**
 * 实例化工具
 * TODO:该方法未完成
 */
var IntantiateData = cc.Class({
    name: 'geoInstantiateData',
    properties: {
        prefab: {
            default: null,
            type: cc.Prefab,
            tooltip: '要被实例化的预制'
        },
        parentNode: {
            default: null,
            type: cc.Node,
            tooltip: '作为实例化对象的父节点，默认为当前节点'
        },
    }
})
var Instantiater = cc.Class({
    extends: cc.Component,

    properties: {
        instantiateDatas:{
            default:[],
            type:[IntantiateData],
        },
        instantiatedEvents: {
            default: [],
            type: [cc.Component.EventHandler],
            tooltip: '实例化后触发的事件'
        },
        instantiateOnLode: {
            default: false,
            tooltip: '是否加载时便实例化'
        }
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        this.instantiateOnLode && this.doInstantiate();
    },

    doInstantiate() {
        this.instantiateDatas.forEach(data => {
            var prefab = data.prefab;
            var parent = data.parentNode || this.node;
            prefab && this.scheduleOnce(this._doInstantiate.bind(this, prefab, parent), 1);
        }, this);
    },
    _doInstantiate(prefab, parent) {
        if (prefab != null) {
            var inst = cc.instantiate(prefab);
            inst.parent = parent;
        }
    }

    // update (dt) {},
});

module.exports = Instantiater;
